/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

using System;
using StandardAnalyzer = Lucene.Net.Analysis.Standard.StandardAnalyzer;
using Document = Lucene.Net.Documents.Document;
using Field = Lucene.Net.Documents.Field;
using Directory = Lucene.Net.Store.Directory;
using RAMDirectory = Lucene.Net.Store.RAMDirectory;
using NUnit.Framework;

namespace Lucene.Net.Index
{
	
	
    [TestFixture]
    public class TestIndexWriterMerging
    {
		
        /// <summary> Tests that index merging (specifically addIndexes()) doesn't
        /// change the index order of documents.
        /// </summary>
        [Test]
        public virtual void  TestLucene()
        {
			
            int num = 100;
			
            Directory indexA = new RAMDirectory();
            Directory indexB = new RAMDirectory();
			
            FillIndex(indexA, 0, num);
            bool fail = VerifyIndex(indexA, 0);
            if (fail)
            {
                Assert.Fail("Index a is invalid");
            }
			
            FillIndex(indexB, num, num);
            fail = VerifyIndex(indexB, num);
            if (fail)
            {
                Assert.Fail("Index b is invalid");
            }
			
            Directory merged = new RAMDirectory();
			
            IndexWriter writer = new IndexWriter(merged, new StandardAnalyzer(), true);
            writer.SetMergeFactor(2);
			
            writer.AddIndexes(new Directory[]{indexA, indexB});
            writer.Close();
            merged.Close();
			
            fail = VerifyIndex(merged, 0);
			
            Assert.IsFalse(fail, "The merged index is invalid");
        }
		
        private bool VerifyIndex(Directory directory, int startAt)
        {
            bool fail = false;
            IndexReader reader = IndexReader.Open(directory);
			
            int max = reader.MaxDoc();
            for (int i = 0; i < max; i++)
            {
                Lucene.Net.Documents.Document temp = reader.Document(i);
                //System.out.println("doc "+i+"="+temp.getField("count").stringValue());
                //compare the index doc number to the value that it should be
                if (!temp.GetField("count").StringValue().Equals((i + startAt) + ""))
                {
                    fail = true;
                    System.Console.Out.WriteLine("Document " + (i + startAt) + " is returning document " + temp.GetField("count").StringValue());
                }
            }
            return fail;
        }
		
        private void  FillIndex(Directory dir, int start, int numDocs)
        {
			
            IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true);
            writer.SetMergeFactor(2);
            writer.SetMaxBufferedDocs(2);
			
            for (int i = start; i < (start + numDocs); i++)
            {
                Lucene.Net.Documents.Document temp = new Lucene.Net.Documents.Document();
                temp.Add(new Field("count", ("" + i), Field.Store.YES, Field.Index.UN_TOKENIZED));
				
                writer.AddDocument(temp);
            }
            writer.Close();
        }
    }
}